<?php


namespace App\Http\Controllers\Admin\System;

use App\Http\Controllers\Admin\BaseController;
use App\Http\Validates\Admin\System\ConfigValidates;
use App\Models\System\SystemConfigModel;
use App\Service\Command\Admin\System\ConfigCommandService;
use App\Service\Query\Admin\System\ConfigQueryService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

class ConfigController extends BaseController
{
    protected $model= SystemConfigModel::class;
    protected $validate = ConfigValidates::class;
    protected $queryService = ConfigQueryService::class;
    protected $commandService = ConfigCommandService::class;
    protected $viewAuthRoute = ['add'=>'/system/config/add','update'=>'/system/config/update'];

    //列表信息
    public function index()
    {
        return $this->lists_index();
    }

    public function update(Request $request)
    {
        if ( $request->ajax() ) {
            return parent::update($request); // TODO: Change the autogenerated stub
        }

        $this->model = app($this->model);
        $info = $this->model->find($request->input('id'));

        if (empty($info)) {
            abort(404);
        }

        if (  SystemConfigModel::TYPE['text'] == $info->type  ) {

            return view('admin.system.config.text_form',['info'=>$info]);
        } elseif ( SystemConfigModel::TYPE['array'] == $info->type ) {

            $info->value = json_decode($info->value,true);
            return view('admin.system.config.array_form',['info'=>$info]);
        } elseif ( SystemConfigModel::TYPE['map'] == $info->type ) {

            $info->value = json_decode($info->value,true);
            return view('admin.system.config.map_form',['info'=>$info]);
        } elseif ( SystemConfigModel::TYPE['img'] == $info->type ) {

            $info->img_src = Config::get('filesystems.disks.public.url'). $info->value;
            return view('admin.system.config.img_form',['info'=>$info]);
        } elseif ( SystemConfigModel::TYPE['switch'] == $info->type ) {

            return view('admin.system.config.switch_form',['info'=>$info]);
        }

    }


}